% Obtain set of expected surpluses / revenues / equilibrium outcomes of platform configuration given
% fees, listing-level surpluses, values, sale probabilities, prices, volumes, all
% pre-calculated with piBS_MC(). 
% ---------------------------------------------- %
function[Pip,Pib,Piwb,Pis,winbid,highval,reservesale,saleprob,Volume,BSWelfare,Totalwelfare,Piwb_posr,Piwb_nor,Pis_posr,Pis_nor,winbid_posr,winbid_nor,saleprob_posr,saleprob_nor]= ...
    Piplatform(Fees,Outcomes,PAR,pipmat,pip0vec,pdfmat,v0vec,...
    pibmat,pib0vec,pismat,pis0mat,pricemat,highvalmat,reservemat,saleprobmat,price0vec,highval0vec,volumemat,volume0vec) 

        x          = length(v0vec(v0vec<=Outcomes.u0star));
        pipfunc    = @(n,which) sum(which(n,1:x))/sum(pdfmat(n,1:x));
        maxn       = PAR.maxn;
        
        % Estimated number of bidder / listing distribution   
        probnposr  = pdf('pois',1:maxn,Outcomes.lambdastarposr).*(1-Outcomes.sharenobidders); 
        probn0posr = pdf('pois',0,Outcomes.lambdastarposr)*(1-Outcomes.sharenobidders)+Outcomes.sharenobidders;
        probnnor   = pdf('pois',1:maxn,Outcomes.lambdastarnor);
        probn0nor  = pdf('pois',0,Outcomes.lambdastarnor);
      
        % Initialize matrices
        Pipgrid         = zeros(PAR.maxn+1,1);
        Pipgridnor      = zeros(PAR.maxn+1,1);
        Pibgrid         = zeros(maxn+1,1);
        Pibgridnor      = zeros(maxn+1,1);
        Piwbgrid        = zeros(maxn+1,1);
        Piwbgridnor     = zeros(maxn+1,1);
        Pisgrid         = zeros(maxn+1,1);
        Pisgridnor      = zeros(maxn+1,1);
        winbidgrid      = zeros(maxn+1,1);
        winbidgridnor   = zeros(maxn+1,1);
        highvalgrid     = zeros(maxn+1,1);
        highvalgridnor  = zeros(maxn+1,1);
        reservesalegrid = zeros(maxn+1,1);
        saleprobgrid    = zeros(maxn+1,1);
        volumegrid      = zeros(maxn+1,1);
        volumegridnor   = zeros(maxn+1,1);
     
         for i = 1:maxn
            Pipgrid(i)         = pipfunc(i,pipmat)*probnposr(i);                                                        % For optimal reserve price
            Pipgridnor(i)      = pip0vec(i)*probnnor(i);
            Pibgrid(i)         = pipfunc(i,pibmat)*probnposr(i);
            Piwbgrid(i)        = pipfunc(i,pibmat)*i*probnposr(i);
            Pisgrid(i)         = pipfunc(i,pismat.*pdfmat)*probnposr(i);
            winbidgrid(i)      = pipfunc(i,pricemat)*probnposr(i);
            highvalgrid(i)     = pipfunc(i,highvalmat)*probnposr(i);
            reservesalegrid(i) = pipfunc(i,reservemat)*probnposr(i);
            saleprobgrid(i)    = pipfunc(i,saleprobmat)*probnposr(i);
            volumegrid(i)      = pipfunc(i,volumemat)*probnposr(i);

            % For sellers setting no reserve price
            Pibgridnor(i)     = pib0vec(i)*probnnor(i);
            Piwbgridnor(i)    = pib0vec(i)*i*probnnor(i);
            Pisgridnor(i)     = pipfunc(i,pis0mat.*pdfmat)*probnnor(i);
            winbidgridnor(i)  = price0vec(i)*probnnor(i);
            highvalgridnor(i) = highval0vec(i)*probnnor(i);
            volumegridnor(i)  = volume0vec(i)*probnnor(i);
        end

        % Expected platform revenues
        Pip  = Outcomes.T*Outcomes.p_r0*(sum(Pipgridnor)./(sum(probnnor)+probn0nor) + (Fees.cL)) +...
               Outcomes.T*(1-Outcomes.p_r0)*(sum(Pipgrid)./(sum(probnposr)+probn0posr) + Fees.cL + Fees.cR) + Outcomes.M*Fees.cP;
       
        % Expected bidder surplus    
        Pib  = Outcomes.M*(1-Outcomes.p_r0)*(sum(Pibgrid)./(sum(probnposr)+probn0posr) - Fees.eBo_posr - Fees.cP)+...
               Outcomes.M*Outcomes.p_r0*(sum(Pibgridnor)./(sum(probnnor)+probn0nor) - Fees.eBo_nor - Fees.cP);
           
        % Expected winning bidder surplus
        Piwb = Outcomes.T*(1-Outcomes.p_r0)*(sum(Piwbgrid)./(sum(probnposr)+probn0posr) - Fees.eBo_posr - Fees.cP)+...
               Outcomes.T*Outcomes.p_r0*(sum(Piwbgridnor)./(sum(probnnor)+probn0nor) - Fees.eBo_nor - Fees.cP);
           
        %r=0,r>0
        Piwb_posr  = Outcomes.T*(1-Outcomes.p_r0)*(sum(Piwbgrid)./(sum(probnposr)+probn0posr) - Fees.eBo_posr - Fees.cP);
        Piwb_nor = Outcomes.T*Outcomes.p_r0*(sum(Piwbgridnor)./(sum(probnnor)+probn0nor) - Fees.eBo_nor - Fees.cP);
       
        % Expected seller surplus   
        Pis  = Outcomes.T*(1-Outcomes.p_r0)*(sum(Pisgrid)./(sum(probnposr)+probn0posr)-Fees.cL-Fees.cR-Fees.eSo)+...
               Outcomes.T*Outcomes.p_r0*(sum(Pisgridnor)./(sum(probnnor)+probn0nor)-Fees.cL-Fees.eSo);
         %r=0,r>0
        Pis_posr = Outcomes.T*(1-Outcomes.p_r0)*(sum(Pisgrid)./(sum(probnposr)+probn0posr)-Fees.cL-Fees.cR-Fees.eSo);
        Pis_nor  = Outcomes.T*Outcomes.p_r0*(sum(Pisgridnor)./(sum(probnnor)+probn0nor)-Fees.cL-Fees.eSo);
      
        BSWelfare    = Piwb + Pis;
        Totalwelfare = Piwb + Pis + Pip; 
        
        % Expected volume
        Volume = Outcomes.T*(1-Outcomes.p_r0)*(sum(volumegrid)./(sum(probnposr)+probn0posr))+...
                 Outcomes.T*Outcomes.p_r0*(sum(volumegridnor)./(sum(probnnor)+probn0nor));
          
        % Calculate separate components going into welfare 
        winbid      = (1-Outcomes.p_r0)*sum(winbidgrid)/sum(probnposr)  +Outcomes.p_r0*sum(winbidgridnor)/sum(probnnor) ;         % Conditional on n> 0 (sale)
        highval     = (1-Outcomes.p_r0)*sum(highvalgrid)/sum(probnposr) +Outcomes.p_r0*sum(highvalgridnor)/sum(probnnor);         % Conditional on n> 0
        reservesale = (1-Outcomes.p_r0)*sum(reservesalegrid)  ;                                                                   % Conditional on n> 0
        saleprob    = (1-Outcomes.p_r0)*sum(saleprobgrid)./(sum(probnposr)+probn0posr) +Outcomes.p_r0*(1-probn0nor) ;             % Conditional on n> 0
        
        winbid_nor    = sum(winbidgridnor)/sum(probnnor);
        winbid_posr   = sum(winbidgrid)/sum(probnposr);
        saleprob_posr  = sum(saleprobgrid)./(sum(probnposr)+probn0posr);
        saleprob_nor = 1-probn0nor;
  
end